#include <iostream>
#include <algorithm>
#include "GraphOrientedList.h"

using namespace std;

GraphOrientedList::GraphOrientedList() : connexions(NULL) {}
GraphOrientedList::GraphOrientedList(const GraphOriented &g)
{
  summit_nb = g.getSummitNb();
  edges_nb = g.getEdgesNb();
  connexions = new vector<summit_t>[summit_nb];
  vector<summit_t> * temp;
  for (summit_t i(0); i<summit_nb; i++)
  {
    temp = g.getNeighbours(i);
    for (int j=0; i<temp->size(); j++)
    {
      connexions[i].push_back(temp->at(i));
    }
    delete temp;
  }
}

GraphOrientedList::GraphOrientedList(summit_t n, edges_t m) : GraphOriented(n,m)
{
  summit_nb = n;
  edges_nb = m;
  connexions = new vector<summit_t>[summit_nb];
}

GraphOrientedList::~GraphOrientedList()
{
  if (connexions)
    delete [] connexions;
}

bool GraphOrientedList::areConnected(summit_t i, summit_t j) const
{
  for (size_t k=0; k<connexions[i].size(); k++)
  {
    if (connexions[i][k]==j)
      return true;
  }
  return false;
}

void GraphOrientedList::connect(summit_t i, summit_t j)
{
  if (not (find(connexions[i].begin(),connexions[i].end(),j) != connexions[i].end()))
  {
    connexions[i].push_back(j);
  }
}

void GraphOrientedList::deconnect(summit_t i, summit_t j)
{
  for (size_t k=0; k<connexions[i].size(); k++)
  {
    if (connexions[i][k]==j) {
      connexions[i].erase(connexions[i].begin()+k);
      return;
    }
  }
}
